//
//  HJJSmallPlayerView.m
//  Pianke
//
//  Created by 黄俊杰 on 15/12/30.
//  Copyright © 2015年 dada. All rights reserved.
//

#import "HJJSmallPlayerView.h"

@implementation HJJSmallPlayerView

+(instancetype)shareSmallPlayerView{
    static HJJSmallPlayerView *shareSmallPlayerV = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        shareSmallPlayerV = [[HJJSmallPlayerView alloc]initWithFrame:CGRectMake(0, 600 * EACH_HEIGHT, 375 * EACH_WIDTH, 67 * EACH_HEIGHT)];
        [shareSmallPlayerV createSmallView];
    });
    return shareSmallPlayerV;
}

-(void)createSmallView{
    
    self.firstBtn = 0;
    self.key = 1;
    
    //签代理
    [HJJPlayerViewController sharePlayerViewController].playerViewDele = self;
    
    self.backgroundColor = [UIColor colorWithRed:0.89 green:0.93 blue:0.96 alpha:1];
    
    self.startBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.startBtn addTarget:self action:@selector(startAction) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.startBtn];
    [self.startBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.mas_equalTo(-110);
        make.top.mas_equalTo(10);
        make.size.mas_equalTo(CGSizeMake(50, 50));
    }];
}

-(void)startAction{

    if (self.firstBtn != 0) {
        
        if ([self.State isEqualToString:@"play"]) {
            
            [self.startBtn setImage:[UIImage imageNamed:@"pause3.png"] forState:UIControlStateNormal];
            [[HJJPlayerViewController sharePlayerViewController] sendSmallState:@"play"];
            self.State = @"pause";
            
        }else{
        
            [self.startBtn setImage:[UIImage imageNamed:@"play2.png"] forState:UIControlStateNormal];
            [[HJJPlayerViewController sharePlayerViewController] sendSmallState:@"pause"];
            self.State = @"play";
        }
        
    }else{
    
        NSLog(@"点点点");
    }
    
    
}

-(void)backState:(NSString *)State{

    NSLog(@"哈哈哈哈哈");
    if ([State isEqualToString:@"play"]){
        
        [self.startBtn setImage:[UIImage imageNamed:@"pause3.png"] forState:UIControlStateNormal];
        self.State = @"pause";
    }else{
    
        [self.startBtn setImage:[UIImage imageNamed:@"play2.png"] forState:UIControlStateNormal];
        self.State = @"play";
    }
    if (self.key == 1) {
        [self.startBtn setImage:[UIImage imageNamed:@"pause3.png"] forState:UIControlStateNormal];
        self.State = @"pause";
        self.key = 0;
    }
}

//代理方法
-(void)backArray:(NSMutableArray *)array indexRow:(NSInteger)indexRow firstBtn:(BOOL)firstBtn{
    
    self.firstBtn = firstBtn;
    if ([self.State isEqualToString:@"play"]){
        
        [self.startBtn setImage:[UIImage imageNamed:@"pause3.png"] forState:UIControlStateNormal];
    }else{
        
        [self.startBtn setImage:[UIImage imageNamed:@"play2.png"] forState:UIControlStateNormal];
    }

    self.myScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 210 * EACH_WIDTH, 67 * EACH_HEIGHT)];
    self.myScroll.backgroundColor = [UIColor colorWithRed:0.89 green:0.93 blue:0.96 alpha:1];
    self.myScroll.delegate = self;
    self.myScroll.userInteractionEnabled = 1;
    self.myScroll.pagingEnabled = 1;
    [self addSubview:self.myScroll];
    
    //点击
    HJJTapGestureRecognizer *tap = [[HJJTapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    [self.myScroll addGestureRecognizer:tap];
    self.myScroll.showsHorizontalScrollIndicator = NO;
    [self.myScroll setContentSize:CGSizeMake(210 * EACH_WIDTH * array.count, 67 * EACH_HEIGHT)];
    [self.myScroll setContentOffset:CGPointMake(210 * EACH_WIDTH *indexRow, 0)];
    //图片和label
    for (int i = 0; i < array.count; i++) {
        
        HJJRadioRootModel *model = [array objectAtIndex:i];
        
        self.picImage = [[UIImageView alloc] initWithFrame:CGRectMake(5 * EACH_WIDTH + i * 210 * EACH_WIDTH, 10, 50 * EACH_WIDTH, 50 * EACH_HEIGHT)];
        self.picImage.layer.masksToBounds = YES;
        self.picImage.layer.cornerRadius = 25;
        self.picImage.backgroundColor = [UIColor colorWithRed:0.16 green:0.24 blue:0.31 alpha:1];
        [self.myScroll addSubview:self.picImage];
        
        self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(60 * EACH_WIDTH + 210 * EACH_WIDTH * i, 10 *EACH_HEIGHT, 130 * EACH_WIDTH, 25 * EACH_HEIGHT)];
        self.titleLabel.font = MIDDLEFONT;
        self.titleLabel.textColor = [UIColor colorWithRed:0.15 green:0.76 blue:0.48 alpha:1];
        [self.myScroll addSubview:self.titleLabel];
        
        self.nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(64 * EACH_WIDTH + 210 * EACH_WIDTH * i, 30 *EACH_HEIGHT, 130 * EACH_WIDTH, 25 * EACH_HEIGHT)];
        self.nameLabel.font = SMALLFONT;
        self.nameLabel.textColor = BLUECOLOR;
        [self.myScroll addSubview:self.nameLabel];
        
        //赋值
        NSURL *picUrl = [NSURL URLWithString:model.coverimg];
        [self.picImage sd_setImageWithURL:picUrl placeholderImage:nil];
        self.titleLabel.text = model.title;
        self.nameLabel.text = model.uname;
    }

}

-(void)tapAction:(HJJTapGestureRecognizer *)tap{

    [self.smallViewDele musicAction];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{

    [HJJPlayerViewController sharePlayerViewController].smallPlayerKey = 1;
    [[HJJPlayerViewController sharePlayerViewController] getSmallPlayerData:self.myScroll.contentOffset.x / (210 * EACH_WIDTH)];
    [self.startBtn setImage:[UIImage imageNamed:@"pause3.png"] forState:UIControlStateNormal];
    self.State = @"Pause";
    
}




@end
